package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.project.system.domain.UserCardVoucher;
import com.happy.cat.project.system.service.IUserCardVoucherService;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 用户卡券Controller
 *
 * @author verne
 * @date 2020-08-13
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userCardVoucher" )
@Api("用户卡券 API")
public class UserCardVoucherController extends BaseController {

    private final IUserCardVoucherService iUserCardVoucherService;

    /**
     * 查询用户卡券列表
     */
    @ApiOperation(value = "查询用户卡券列表",notes = "查询用户卡券列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userCardVoucher:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCardVoucher userCardVoucher)
    {
        startPage();
        LambdaQueryWrapper<UserCardVoucher> lqw = new LambdaQueryWrapper<UserCardVoucher>();
        if (StringUtils.isNotBlank(userCardVoucher.getCardVoucherName())){
            lqw.like(UserCardVoucher::getCardVoucherName ,userCardVoucher.getCardVoucherName());
        }
        if (userCardVoucher.getCardVoucherType() != null){
            lqw.eq(UserCardVoucher::getCardVoucherType ,userCardVoucher.getCardVoucherType());
        }
        if (StringUtils.isNotBlank(userCardVoucher.getUseRule())){
            lqw.eq(UserCardVoucher::getUseRule ,userCardVoucher.getUseRule());
        }
        if (userCardVoucher.getCardAmount() != null){
            lqw.eq(UserCardVoucher::getCardAmount ,userCardVoucher.getCardAmount());
        }
        if (userCardVoucher.getEffectiveTime() != null){
            lqw.eq(UserCardVoucher::getEffectiveTime ,userCardVoucher.getEffectiveTime());
        }
        if (userCardVoucher.getInvalidTime() != null){
            lqw.eq(UserCardVoucher::getInvalidTime ,userCardVoucher.getInvalidTime());
        }
        if (userCardVoucher.getAccountId() != null){
            lqw.eq(UserCardVoucher::getAccountId ,userCardVoucher.getAccountId());
        }
        List<UserCardVoucher> list = iUserCardVoucherService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出用户卡券列表
     */
    @ApiOperation(value = "导出用户卡券列表",notes = "导出用户卡券列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userCardVoucher:export')" )
    @Log(title = "用户卡券" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(UserCardVoucher userCardVoucher) {
        LambdaQueryWrapper<UserCardVoucher> lqw = new LambdaQueryWrapper<UserCardVoucher>(userCardVoucher);
        List<UserCardVoucher> list = iUserCardVoucherService.list(lqw);
        ExcelUtil<UserCardVoucher> util = new ExcelUtil<UserCardVoucher>(UserCardVoucher. class);
        return util.exportExcel(list, "userCardVoucher" );
    }

    /**
     * 获取用户卡券详细信息
     */
    @ApiOperation(value = "获取用户卡券详细信息",notes = "获取用户卡券详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userCardVoucher:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iUserCardVoucherService.getById(id));
    }

    /**
     * 新增用户卡券
     */
    @ApiOperation(value = "新增用户卡券",notes = "新增用户卡券",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userCardVoucher:add')" )
    @Log(title = "用户卡券" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCardVoucher userCardVoucher) {
        return toAjax(iUserCardVoucherService.save(userCardVoucher) ? 1 : 0);
    }

    /**
     * 修改用户卡券
     */
    @ApiOperation(value = "修改用户卡券",notes = "修改用户卡券",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userCardVoucher:edit')" )
    @Log(title = "用户卡券" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCardVoucher userCardVoucher) {
        return toAjax(iUserCardVoucherService.updateById(userCardVoucher) ? 1 : 0);
    }

    /**
     * 删除用户卡券
     */
    @ApiOperation(value = "删除用户卡券",notes = "删除用户卡券",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userCardVoucher:remove')" )
    @Log(title = "用户卡券" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iUserCardVoucherService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
